import {FC, ReactElement} from "react";
import {useNavigate} from "react-router-dom";
import {SafeArea, NavBar, NavBarProps, PullToRefresh, DotLoading} from "antd-mobile";
import {Icon} from "..";
import "./index.less";

interface ContainerProps extends NavBarProps {
    className?: string;
    sticky?: boolean;
    completeText?: string;
    bottom?: ReactElement | any;
    children?: ReactElement | any;
    title?: ReactElement | any;
    backArrow?: ReactElement | string;
    refresh?: boolean;
    onBack?: () => any;
    onRefresh?: () => any;
}

const Container: FC<ContainerProps> = ({
    back = "",
    title = "",
    right = "",
    bottom = "",
    children = "",
    className = "",
    backArrow = "",
    completeText = "",
    sticky = false,
    refresh = false,
    onBack,
    onRefresh,
}) => {
    const nav = useNavigate();
    const handleBack = () => {
        if (onBack) {
            onBack();
        } else {
            nav(-1);
        }
    };

    return (
        <PullToRefresh
            disabled={refresh}
            completeText={completeText}
            onRefresh={() => onRefresh?.()}
            refreshingText={<DotLoading color="#ef180c" />}
            canReleaseText={<Icon size={32} className="icon-down" />}
        >
            <SafeArea position="top" />
            <div className={`Container ${className}`}>
                <div className={`${sticky ? "sticky" : ""}`}>
                    {title && (
                        <NavBar
                            {...(backArrow ? {backArrow} : {backArrow: <Icon className="icon-left" />})}
                            className="nav-bar"
                            back={back}
                            onBack={handleBack}
                            right={right}
                        >
                            {title}
                        </NavBar>
                    )}
                    {bottom}
                </div>
                {children}
            </div>
        </PullToRefresh>
    );
};

export default Container;
